<?php

namespace app\courier\controller;

use app\model\CourierBank as ModelCourierBank;
use app\model\CourierInfo as ModelCourierInfo;
use app\logic\sms\SMS as LogicSms;
use app\logic\BankCard as LogicBankCard;

class BankCard extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 快递员银行卡列表
     * URI：/courier/bankCard.list
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bankCardList()
    {
        $int_courier_id = $this->courier_id;

        $where_arr = [
            'map_id' => $int_courier_id,
            'state' => 1,
            'type' => 1
        ];
        $field = 'id,card_logo,bank_name,card_number';
        $list = ModelCourierBank::getInstance()->toSelect($where_arr, $field, [], [], ['gmt_create' => 'desc']);

        _returnData(['bankList' => $list]);
    }

    /*
     * 删除银行卡
     * URI：/courier/bankCard.del
     */
    public function bankCardDel()
    {
        $int_id = $this->_getContentParams('id');
        $int_courier_id = $this->courier_id;
        //验证银行卡id
        $check_res = ModelCourierBank::getInstance()->toFind(['id' => $int_id]);
        if (!$check_res) _returnError('15004');

        $where_arr = [
            'id' => $int_id,
            'map_id' => $int_courier_id,
            'type' => 1
        ];
        $upData = [
            'state' => 5
        ];

        $res = ModelCourierBank::getInstance()->toUpdate($where_arr, $upData);
        if ($res) _returnData();
        _returnError('10012');

    }

    /**
     * 添加银行卡
     * URI：/courier/bankCard.add
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bankCardAdd()
    {
        $str_real_name = $this->_getContentParams('real_name');
        $str_card_number = $this->_getContentParams('cardNumber');
        $str_mobile = $this->_getContentParams('mobile');
        $str_code = $this->_getContentParams('code');
        $str_pass_key = $this->_getContentParams('pass_key');

        _toCompare($str_real_name, '==', '', '15024');
        //获取快递员信息
        $field_info = "id,real_name,state,auth_state";
        $dataInfo = ModelCourierInfo::getInstance()->toFind(['id' => $this->courier_id], $field_info);
        //判断用户是否已经实名认证
        _toCompare($dataInfo['auth_state'], '==', '0', 15001);
        //判断姓名
        _toCompare($dataInfo['real_name'], '!=', $str_real_name, 15002); //该银行卡姓名与真实姓名不符
        //判断卡号
        _toCompare($str_card_number, '==', '', 15003); //请输入您的银行卡号
        if (!preg_match('/^\d*$/', $str_card_number)) {
            _returnError('15005');//银行卡号不合法
        }

        //判断卡号是否重复
        $cardList = ModelCourierBank::getInstance()->toSelect(['state' => 1, 'type' => 1, 'map_id' => $this->courier_id]);
        foreach ($cardList as $key => $value) {
            _toCompare($str_card_number, '==', $value['card_number'], 15006);
        }
        //验证验证码

        if (empty($str_code)) _returnError('19002');
        $res_code = LogicSms::getInstance()->verifySMS('bank_card', $str_mobile, $str_pass_key, $str_code, 'courier');
        _toCompare($res_code, '===', false, 11005); // 验证码不正确

        //获取用户身份证号
        $id_number = ModelCourierInfo::getInstance()->toValue(['id' => $this->courier_id], 'id_number');
        //验证银行卡四要素
        $res_boll = LogicBankCard::getInstance()->aliYunVerifyBankCard4($str_real_name, $id_number, $str_card_number, $str_mobile);
        _toCompare($res_boll,'==',false,15028);

        //获取卡片信息
        $cardInfo = LogicBankCard::getInstance()->aliYunCheckBankCardType($str_card_number);
        //写入数据库
        $data['card_logo'] = $cardInfo['logo'];
        $data['bank_name'] = $cardInfo['bank'];
        $data['card_number'] = $str_card_number;
        $data['type'] = 1;
        $data['map_id'] = $this->courier_id;
        $data['real_name'] = $dataInfo['real_name'];
        $data['mobile'] = $str_mobile;
        $add_res = ModelCourierBank::getInstance()->toAdd($data);
        if ($add_res) _returnData();
        _returnError(10010);
    }
}
